classdef PNA < NVNA
     
    methods
        
        function this = PNA(GPIB_controller, GPIB_address, address)
        
            this = this@NVNA(GPIB_controller, GPIB_address, address);
            fopen(this.conn);
            fprintf(this.conn,'*RST');
        end
        
        function delete(this)
            fprintf(this.conn,'*RST');
            fclose(this.conn);
            delete(this.conn);
        end
        
        function setFreq(this)
            
            cmd = sprintf('SENSE1:FREQ:CW %fGHz',this.frequency);
            fprintf(this.conn,cmd);
        end
        
        function setStartFreq(this)
            
            
            cmd = sprintf('SENSE1:FREQ:START %fGHz',this.startFrequency);
            fprintf(this.conn,cmd);
        end
                  
        function setStopFreq(this)
            
            
            cmd = sprintf('SENSE1:FREQ:STOP %fGHz',this.stopFrequency);
            fprintf(this.conn,cmd);
        end
          
        function setPower(this)
            %set output power i dBm
            
            
            cmd = sprintf('SOURCE1:POWER:LEVEL %f',this.power);
            fprintf(this.conn,cmd);
        end
        
        function setStartPower(this)
            %set output power i dBm
            
            
            cmd = sprintf('SOUR:POW:STAR %f',this.startPower);
            fprintf(this.conn,cmd);
        end
        
        function setStopPower(this)
            %set output power i dBm
            
            
            cmd = sprintf('SOUR:POW:STOP %f',this.stopPower);
            fprintf(this.conn,cmd);
        end
        
        function setSweepPoints(this)
            
            cmd = sprintf('SENSE1:SWEEP:POINTS %d',this.sweepPoints);
            fprintf(this.conn,cmd);
        end
        
        function setResolutionBandwidth(this)
            %set resolution bandwidth of the analyzer ( bandwidth in Hz)
            
            cmd = sprintf('SENSE1:BANDWIDTH:RESOLUTION %f',this.bandwidth);
            fprintf(this.conn,cmd);
        end
                        
        function setSingleSweep(this, singleSweep)
            
            this.singleSweep = singleSweep;
            cmd = sprintf('INIT1:CONT %s',this.singleSweep);
            fprintf(this.conn,cmd);
        end
        
        function setSweepCount(this)
            
            cmd = sprintf('SENSE1:SWEEP:COUNT %d',this.sweepCount);
            fprintf(this.conn,cmd);
            
        end
       
        function setSweepType(this)
            
            cmd = sprintf('SENSE1:SWEEP:TYPE %s',this.sweepType);
            fprintf(this.conn,cmd);
        end
        
        function setDisplayUpdate(this,displayUpdate)
            
            cmd = sprintf('SYST:DISP:UPDATE %s',displayUpdate);
            fprintf(this.conn,cmd);
            
        end
        
            
        function newWindow(this, displayName)
            
            
            cmd = sprintf('MEM:DEF ''%s''',displayName);
            fprintf(this.conn,cmd);
        end
        
        function deleteAllTraces(this)
            
            cmd = sprintf('CALC:PAR:DEL:ALL');
            fprintf(this.conn,cmd);
        end
        
        function addTrace(this,trace_number,trace_name,parameter)
            
            cmd = sprintf('CALC:PAR:EXT ''%s'', ''%s''',trace_name,parameter);
            fprintf(this.conn,cmd);
            cmd = sprintf('DISP:WIND:TRAC%d:FEED ''%s''',trace_number, trace_name);
            fprintf(this.conn,cmd);
        end
        
        function addSParamTraces(this,channel, input_port, output_port)
            
            cmd = sprintf('CALC%d:PAR:DEF:SGR %d,%d',channel, input_port, output_port);
            fprintf(this.conn,cmd);        
        end
        
        function createDiagramArea(this, diagram_number)
            
            cmd = sprintf('DISP:WIND%d:STAT ON',diagram_number);
            fprintf(this.conn,cmd);
        end
        
     
        
        function selectSParameters(this,channel,trace_name,s_parameter)
            
            cmd = sprintf('CALC%d:PAR:MEAS ''%s'', ''%s''',channel,trace_name,s_parameter);
            fprintf(this.conn,cmd);            
        end
        
        function setTraceFormat(this,format)
            
            cmd = sprintf('CALC:FORMAT %s',format);
            fprintf(this.conn,cmd);            
            fprintf(this.conn,':FORM:BORD SWAP');
        end
        
        function trigger(this)
            
%             fprintf(this.conn,'TRIG:SCOP ALL');
%             fprintf(this.conn,'SENS:SWE:MODE SING');
            cmd = sprintf('INIT:IMM');
            fprintf(this.conn,cmd);
            opc = query(this.conn,' *OPC?');
        end
        
        function setAutoScale(this)
            
             cmd = sprintf('DISP:WIND:Y:AUTO');
            fprintf(this.conn,cmd);
        end
        
        function closeWindow(this)
            
            cmd = sprintf('DISP:WIND:STAT OFF');
            fprintf(this.conn,cmd);
        end
        
        function result = getStimulusData(this,channel)
            
            cmd = sprintf('CALC%d:DATA:STIM?',channel);
            fprintf(this.conn,cmd);
            %result = (fscanf(this.conn));
            result=binblockread(this.conn,'float');
            fread(this.conn,1);
            
        end
        
        function result = getResponseData(this,name)
            
            cmd = sprintf('CALC:PAR:SEL ''%s''',name); 
            fprintf(this.conn,cmd);
            cmd = sprintf('CALC:DATA? SDATA'); 
            fprintf(this.conn,cmd);
            out = binblockread(this.conn,'float');
            fread(this.conn,1);
            result = this.parseParameter(1,this.sweepPoints,out,1);
            
        end 
        
        function setLowPhaseNoise(this, channel, value)
            
            cmd = sprintf('SENSE%d:FREQ:LPNOISE %s',channel,value);
            fprintf(this.conn,cmd);
        end
            
        
        
        
        function setDataFormat(this, format)
            
            cmd = sprintf('FORMAT %s' ,format);
            fprintf(this.conn,cmd);
        end
     
    end
    
end

